package org.jeecg.modules.demo.mall.yxstorecategory.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.mall.entity.YxStoreCategory;
import org.jeecg.modules.demo.mall.yxstorecategory.mapper.YxStoreCategoryMapper;
import org.jeecg.modules.demo.mall.yxstorecategory.service.IYxStoreCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: yx_store_category
 * @Author: jeecg-boot
 * @Date:   2023-10-19
 * @Version: V1.0
 */
@Api(tags="商家分类")
@RestController
@RequestMapping("/mall/yxstorecategory/yxStoreCategory")
@Slf4j
public class YxStoreCategoryController extends JeecgController<YxStoreCategory, IYxStoreCategoryService> {
	@Autowired
	private IYxStoreCategoryService yxStoreCategoryService;

	/**
	 * 分页列表查询
	 *
	 * @param yxStoreCategory
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "yx_store_category-分页列表查询")
	@ApiOperation(value="yx_store_category-分页列表查询", notes="yx_store_category-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<YxStoreCategory>> queryPageList(YxStoreCategory yxStoreCategory,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<YxStoreCategory> queryWrapper = QueryGenerator.initQueryWrapper(yxStoreCategory, req.getParameterMap());
		if (StringUtils.isNotBlank(yxStoreCategory.getMerId())){
			queryWrapper.eq("mer_id",yxStoreCategory.getMerId());
		}else {
			queryWrapper.isNull("mer_id");
		}
		queryWrapper.orderByDesc("sort");
		Page<YxStoreCategory> page = new Page<YxStoreCategory>(pageNo, pageSize);
		IPage<YxStoreCategory> pageList = yxStoreCategoryService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param yxStoreCategory
	 * @return
	 */
//	@AutoLog(value = "yx_store_category-添加")
	@ApiOperation(value="yx_store_category-添加", notes="yx_store_category-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody YxStoreCategory yxStoreCategory) {
		yxStoreCategoryService.save(yxStoreCategory);
		return Result.OK("添加成功！");
	}

	 /**
	  *   list查询商品分类
	  *
	  * @param
	  * @return
	  */
//	@AutoLog(value = "yx_store_category-添加")
	 @ApiOperation(value="yx_store_category-查询list部不分页", notes="yx_store_category-查询list部不分页")
	 @GetMapping(value = "/queryList")
	 public Result<List<YxStoreCategory>> queryList(String merId) {
	 	QueryWrapper<YxStoreCategory> queryWrapper = new QueryWrapper<>();
	 	if (StringUtils.isNotBlank(merId)){
	 		queryWrapper.eq("mer_id",merId);
		}else {
	 		queryWrapper.isNull("mer_id");
		}
		 List<YxStoreCategory> yxStoreCategories = yxStoreCategoryService.list();
		 return Result.OK(yxStoreCategories);
	 }

	/**
	 *  编辑
	 *
	 * @param yxStoreCategory
	 * @return
	 */
//	@AutoLog(value = "yx_store_category-编辑")
	@ApiOperation(value="yx_store_category-编辑", notes="yx_store_category-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody YxStoreCategory yxStoreCategory) {
		yxStoreCategoryService.updateById(yxStoreCategory);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
//	@AutoLog(value = "yx_store_category-通过id删除")
	@ApiOperation(value="yx_store_category-通过id删除", notes="yx_store_category-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		yxStoreCategoryService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
//	@AutoLog(value = "yx_store_category-批量删除")
	@ApiOperation(value="yx_store_category-批量删除", notes="yx_store_category-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxStoreCategoryService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "yx_store_category-通过id查询")
	@ApiOperation(value="yx_store_category-通过id查询", notes="yx_store_category-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<YxStoreCategory> queryById(@RequestParam(name="id",required=true) String id) {
		YxStoreCategory yxStoreCategory = yxStoreCategoryService.getById(id);
		if(yxStoreCategory==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxStoreCategory);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxStoreCategory
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxStoreCategory yxStoreCategory) {
        return super.exportXls(request, yxStoreCategory, YxStoreCategory.class, "yx_store_category");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxStoreCategory.class);
    }

}
